﻿using Domain.Base;
using Domain.Entity;
using DtoFw;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Repository.Base
{
    public interface IRepository<T> where T : BaseEntity
    {
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="req">基础查询</param>
        /// <param name="funcWhere">查询条件的方法</param>
        /// <param name="database">数据库</param>
        /// <returns></returns>
        ISugarQueryable<T> Query(BaseQuery req = null, Func<ISugarQueryable<T>, ISugarQueryable<T>> funcWhere = null);

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="req">基础查询</param>
        /// <param name="funcWhere">查询条件的方法</param>
        /// <param name="funcSort">排序的方法</param>
        /// <param name="selector"></param>
        /// <param name="orderByType"></param>
        /// <returns></returns>
        Pager<T> GetPager(BaseQuery req = null, Func<ISugarQueryable<T>, ISugarQueryable<T>> funcWhere = null, Expression<Func<T, T>> selector = null, Expression<Func<T, object>> funcSort = null, OrderByType orderByType = OrderByType.Asc);

        /// <summary>
        /// 操作对象
        /// </summary>
        /// <param name="req">操作请求</param>
        /// <param name="action">执行方法</param>
        /// <param name="funcWhere">查询条件</param>
        /// <param name="database">数据库</param>
        /// <param name="isTransaction">是否事物</param>
        void Operate(BaseOperate req, Func<ISugarQueryable<T>, ISugarQueryable<T>> funcWhere = null, Action<T, SqlSugarClient> action = null, bool isTransaction = false);

        /// <summary>
        /// 操作对象
        /// </summary>
        /// <param name="req">操作请求</param>
        /// <param name="action">执行方法</param>
        /// <param name="isTransaction">是否事物</param>
        void Operate(BaseOperate req, Action<T, SqlSugarClient> action, bool isTransaction = false);

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="entity">实体</param>
        /// <param name="action">执行保存前方法</param>
        /// <param name="isTransaction">是否执行事务</param>
        /// <returns></returns>
        BaseEntity Save(T entity, Action<SqlSugarClient> action = null, bool isTransaction = false);

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="req">基础条件</param>
        /// <param name="funcWhere">查询条件</param>
        /// <returns></returns>
        int Delete(BaseOperate req, Func<ISugarQueryable<T>, ISugarQueryable<T>> funcWhere = null, Func<List<T>, List<T>> func = null);

        /// <summary>
        /// 删除
        /// </summary>
        /// <returns></returns>
        int Delete(BaseOperate req);

        /// <summary>
        /// 批量新增
        /// </summary>
        /// <param name="list"></param>
        int InsertBatch(List<T> list);

        /// <summary>
        /// 批量新增
        /// </summary>
        /// <param name="list">集合</param>
        /// <returns></returns>
        public (int result, List<long> ids) InsertBatchReturn(List<T> list);

        /// <summary>
        /// 根据Id批量删除
        /// </summary>
        /// <param name="bases"></param>
        int DeleteBatch(BaseOperate req);

        /// <summary>
        /// 查询返回受影响行数
        /// </summary>
        /// <param name="req"></param>
        /// <param name="funcWhere"></param>
        /// <returns></returns>
        int Count(BaseQuery req = null, Func<ISugarQueryable<T>, ISugarQueryable<T>> funcWhere = null);

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="list">对象</param>
        /// <returns></returns>
        long Insert(T t);

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="list">对象</param>
        /// <returns></returns>
        Task<long> InsertAsync(T t);
    }
}
